﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using XMK.Framework.DataAccess;
using XMK.Tadong.Model;
using XMK.Utils;
using XMK.Framework.Model;
using XMK.Tadong.Model.Cond;


namespace XMK.Tadong.Data
{
  public  class Tb_Person_FileData
    {
       /// <summary>
        /// 数据库连接
        /// </summary>
        protected DbProvider dbProvider;

        public Tb_Person_FileData(DbProvider dbProvider)
        {
            this.dbProvider = dbProvider;

        }

      /// <summary>
      /// 判断本用户下是否已有同名文件
      /// </summary>
      /// <param name="?"></param>
      /// <returns></returns>
      public bool IsExist(TB_PERSON_FILEInfo fileObj)
      {
          string save_path = fileObj.SAVE_PATH;
          string name = fileObj.FILE_NAME;
          string userId = fileObj.USER_ID;
          StringBuilder strSql = new StringBuilder();
          strSql.Append("SELECT count(*) FROM TB_PERSON_FILE WHERE SAVE_PATH = '" + save_path + "' and FILE_NAME='" + name + "' and USER_ID='" + userId + "'");
           DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString());
  
           cmd.DbCmdInfo.CommandType = CommandType.Text;
           object obj= DbManager.ExecScalar(cmd);

           return (Convert.ToInt16( obj) != 0);


      }

      /// <summary>
      /// 判断同一用户，文件夹下是否已有同名文件
      /// </summary>
      /// <param name="?"></param>
      /// <returns></returns>
      public bool HasFileName(string User_ID,string File_Name,string Folder_ID)
      {
          StringBuilder strSql = new StringBuilder();
          strSql.Append("SELECT count(*) FROM TB_PERSON_FILE WHERE User_ID = '" + User_ID + "' and Folder_ID='" + Folder_ID + "'and File_Name='" + File_Name + "'");
          DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString());

          cmd.DbCmdInfo.CommandType = CommandType.Text;
          object obj = DbManager.ExecScalar(cmd);

          return (Convert.ToInt16(obj) != 0);


      }

      /// <summary>
      /// 判断同一目录下是否有多个引用
      /// </summary>
      /// <param name="?"></param>
      /// <returns></returns>
      public bool HasSavePath(string Save_Path)
      {
          StringBuilder strSql = new StringBuilder();
          strSql.Append("SELECT count(*) FROM TB_PERSON_FILE WHERE Save_Path = '" + Save_Path + "'");
          DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString());

          cmd.DbCmdInfo.CommandType = CommandType.Text;
          object obj = DbManager.ExecScalar(cmd);

          return (Convert.ToInt16(obj) > 1);


      }

        /// <summary>
        /// 添加新文件
        /// </summary>
        /// <returns></returns>
        public bool  InsertNewFile(TB_PERSON_FILEInfo fileObj)
        {
            if (fileObj.SECRET_LVL == null)
                fileObj.SECRET_LVL = "10";
            bool result =true;
            string sql = @"insert into TB_PERSON_FILE( USER_ID,   SAVE_PATH,   CREATE_TIME ,  UPD_TIME ,   FILE_NAME,   FILE_EXT,    FILE_ABST,    KEYW,    FILE_CRT_DATE,   FILE_TYPE,   FILE_AUTHOR,FILE_SIZE，DOWN_AMOUNT， DEST_OBJ,    BIZ_OBJ,     SECRET_LVL,     LANG_TYPE,   PDF_PATH,    SWF_PATH,FOLDER_ID,FILE_VERSION)
                                             values(:p_USER_ID,:p_SAVE_PATH,:p_CREATE_TIME,:p_UPD_TIME, :p_FILE_NAME, :p_FILE_EXT, :p_FILE_ABST, :p_KEYW,:p_FILE_CRT_DATE,:p_FILE_TYPE,:p_FILE_AUTHOR,:p_FILE_SIZE,:p_DOWN_AMOUNT,:p_DEST_OBJ,:p_BIZ_OBJ,:p_SECRET_LVL,:p_LANG_TYPE,:p_PDF_PATH,:p_SWF_PATH,:p_FOLDER_ID,:p_FILE_VERSION)";

            //新建一个数据库操作帮助类
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, sql,
                //参数生成
                delegate()
                {
                    return DbCmdHelper.CreateParameters(dbProvider, "USER_ID", "SAVE_PATH", "CREATE_TIME", "UPD_TIME", "FILE_NAME", "FILE_EXT", "FILE_ABST", "KEYW", "FILE_CRT_DATE", "FILE_TYPE", "FILE_AUTHOR", "FILE_SIZE", "DOWN_AMOUNT", "DEST_OBJ", "BIZ_OBJ", "SECRET_LVL", "LANG_TYPE", "PDF_PATH", "SWF_PATH", "FOLDER_ID", "FILE_VERSION").ToArray();
                });
            //参数设值
            cmd.SetParamValues(fileObj.USER_ID, fileObj.SAVE_PATH, fileObj.CREATE_TIME, fileObj.UPD_TIME, fileObj.FILE_NAME, fileObj.FILE_EXT, fileObj.FILE_ABST, fileObj.KEYW, fileObj.FILE_CRT_DATE, fileObj.FILE_TYPE, fileObj.FILE_AUTHOR, fileObj.FILE_SIZE, fileObj.DOWN_AMOUNT, fileObj.DEST_OBJ, fileObj.BIZ_OBJ, fileObj.SECRET_LVL, fileObj.LANG_TYPE, fileObj.PDF_PATH, fileObj.SWF_PATH, fileObj.FOLDER_ID,fileObj.FILE_VERSION);
            cmd.DbCmdInfo.CommandType = CommandType.Text;
            int back= DbManager.ExecNonQuery(cmd);
            if(back==0)
                result=false;

            return result;
             
        }
      /// <summary>
      /// 更新文件记录
      /// </summary>
      /// <param name="fileObj"></param>
      /// <returns></returns>
        public bool Update(TB_PERSON_FILEInfo fileObj)
        {
            bool result = true;
            if (fileObj.FILE_ID == null)
            {
                return false;
            }

            string sql = @"update TB_PERSON_FILE  set USER_ID=:p_USER_ID, SAVE_PATH=:p_SAVE_PATH,CREATE_TIME=:p_CREATE_TIME ,UPD_TIME=:p_UPD_TIME ,  
                            FILE_NAME=:p_FILE_NAME, FILE_EXT=:p_FILE_EXT,  FILE_ABST=:p_FILE_ABST,  KEYW=:p_KEYW, FILE_CRT_DATE=:p_FILE_CRT_DATE, FILE_TYPE=:p_FILE_TYPE,FILE_AUTHOR=:p_FILE_AUTHOR,FILE_VERSION=:p_FILE_VERSION,FILE_SIZE=:p_FILE_SIZE,DOWN_AMOUNT=:p_DOWN_AMOUNT,DEST_OBJ=:p_DEST_OBJ,BIZ_OBJ=:p_BIZ_OBJ,SECRET_LVL=:p_SECRET_LVL,LANG_TYPE=:p_LANG_TYPE,PDF_PATH=:p_PDF_PATH,SWF_PATH=:p_SWF_PATH,
                            FOLDER_ID=:p_FOLDER_ID  where FILE_ID=:p_FILE_ID ";

            //新建一个数据库操作帮助类
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, sql,
                //参数生成
                delegate()
                {
                    return DbCmdHelper.CreateParameters(dbProvider, "USER_ID", "SAVE_PATH", "CREATE_TIME", "UPD_TIME", "FILE_NAME", "FILE_EXT", "FILE_ABST", "KEYW", "FILE_CRT_DATE", "FILE_TYPE", "FILE_ID", "FILE_AUTHOR", "FILE_VERSION", "FILE_SIZE", "DOWN_AMOUNT", "DEST_OBJ", "BIZ_OBJ", "SECRET_LVL", "LANG_TYPE", "PDF_PATH", "SWF_PATH", "FOLDER_ID").ToArray();
                });
            //参数设值
            cmd.SetParamValues(fileObj.USER_ID, fileObj.SAVE_PATH, fileObj.CREATE_TIME, fileObj.UPD_TIME, fileObj.FILE_NAME, fileObj.FILE_EXT, fileObj.FILE_ABST, fileObj.KEYW, fileObj.FILE_CRT_DATE, fileObj.FILE_TYPE, fileObj.FILE_ID, fileObj.FILE_AUTHOR, fileObj.FILE_VERSION, fileObj.FILE_SIZE, fileObj.DOWN_AMOUNT, fileObj.DEST_OBJ, fileObj.BIZ_OBJ, fileObj.SECRET_LVL, fileObj.LANG_TYPE, fileObj.PDF_PATH, fileObj.SWF_PATH, fileObj.FOLDER_ID);
            cmd.DbCmdInfo.CommandType = CommandType.Text;
            int back = DbManager.ExecNonQuery(cmd);
            return result;
        }

//        /// <summary>
//        ///共享文件
//        /// </summary>
//        /// <returns></returns>
//        public bool ShareFile(TB_PERSON_FILE fileObj)
//        {
//            bool result = true;
//            string sql = @"insert into TB_PERSON_FILE(USER_ID, SAVE_PATH,CREATE_TIME ,UPD_TIME ,  FILE_NAME,  FILE_EXT,  FILE_ABST,  KEYW, FILE_CRT_DATE, FILE_TYPE,FILE_AUTHOR,FILE_VERSION,DEST_OBJ,BIZ_OBJ,SECRET_LVL,LANG_TYPE)
//                                             values(:p_USER_ID,:p_SAVE_PATH,:p_CREATE_TIME,:p_UPD_TIME, :p_FILE_NAME, :p_FILE_EXT, :p_FILE_ABST, :p_KEYW,:p_FILE_CRT_DATE,:p_FILE_TYPE,:p_FILE_AUTHOR,:p_FILE_VERSION,:p_DEST_OBJ,BIZ_OBJ,:p_SECRET_LVL,:p_LANG_TYPE)";

//            //新建一个数据库操作帮助类
//            DbCmdHelper cmd = new DbCmdHelper(dbProvider, sql,
//                //参数生成
//                delegate()
//                {
//                    return DbCmdHelper.CreateParameters(dbProvider, "USER_ID", "SAVE_PATH", "CREATE_TIME", "UPD_TIME", "FILE_NAME", "FILE_EXT", "FILE_ABST", "KEYW", "FILE_CRT_DATE", "FILE_TYPE", "FILE_AUTHOR", "FILE_VERSION", "DEST_OBJ", "BIZ_OBJ", "SECRET_LVL", "LANG_TYPE").ToArray();
//                });
//            //参数设值
//            cmd.SetParamValues(fileObj.USER_ID, fileObj.SAVE_PATH, fileObj.CREATE_TIME, fileObj.UPD_TIME, fileObj.FILE_NAME, fileObj.FILE_EXT, fileObj.FILE_ABST, fileObj.KEYW, fileObj.FILE_CRT_DATE, fileObj.FILE_TYPE, fileObj.FILE_AUTHOR, fileObj.FILE_VERSION, fileObj.DEST_OBJ, fileObj.BIZ_OBJ, fileObj.SECRET_LVL, fileObj.LANG_TYPE);
//            cmd.DbCmdInfo.CommandType = CommandType.Text;
//            int back = DbManager.ExecNonQuery(cmd);
//            if (back == 0) 
//                result = false;

//            return result;

//        }

        public TB_PERSON_FILEInfo GetFileInfoById(string fileId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"SELECT a.*,b.user_name FROM TB_PERSON_FILE a
                            LEFT JOIN BASE_USERINFO b on a.user_id = b.user_id
                            WHERE FILE_ID =" + fileId);

            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString());
            cmd.DbCmdInfo.CommandType = CommandType.Text;
            TB_PERSON_FILEInfo fileObj = DbManager.GetInfo(cmd,
                delegate(out TB_PERSON_FILEInfo u, DataRow row)
                {
                    u = new TB_PERSON_FILEInfo
                    {
                        FILE_ID = ParseHelper.Parse<decimal>(row["FILE_ID"]),
                        FILE_NAME = ParseHelper.Parse<string>(row["FILE_NAME"]),
                        FILE_EXT = ParseHelper.Parse<string>(row["FILE_EXT"]),
                        FILE_CRT_DATE = ParseHelper.Parse<DateTime>(row["FILE_CRT_DATE"]),
                        KEYW = ParseHelper.Parse<string>(row["KEYW"]),
                        UPD_TIME = ParseHelper.Parse<DateTime>(row["UPD_TIME"]),
                        FILE_TYPE = ParseHelper.Parse<string>(row["FILE_TYPE"]),
                        FILE_ABST = ParseHelper.Parse<string>(row["FILE_ABST"]),
                        CREATE_TIME = ParseHelper.Parse<DateTime>(row["CREATE_TIME"]),
                        SAVE_PATH = ParseHelper.Parse<string>(row["SAVE_PATH"]),
                        USER_ID = ParseHelper.Parse<string>(row["USER_ID"]),
                        FILE_AUTHOR = ParseHelper.Parse<string>(row["FILE_AUTHOR"]),
                        FILE_VERSION = ParseHelper.Parse<decimal>(row["FILE_VERSION"]),
                        FILE_SIZE = ParseHelper.Parse<string>(row["FILE_SIZE"]),
                        DOWN_AMOUNT = ParseHelper.Parse<decimal>(row["DOWN_AMOUNT"]),
                        DEST_OBJ = ParseHelper.Parse<string>(row["DEST_OBJ"]),
                        BIZ_OBJ = ParseHelper.Parse<string>(row["BIZ_OBJ"]),
                        SECRET_LVL = ParseHelper.Parse<string>(row["SECRET_LVL"]),
                        LANG_TYPE = ParseHelper.Parse<decimal>(row["LANG_TYPE"]),
                        PDF_PATH = ParseHelper.Parse<string>(row["PDF_PATH"]),
                        SWF_PATH = ParseHelper.Parse<string>(row["SWF_PATH"]),
                        FOLDER_ID = ParseHelper.Parse<string>(row["FOLDER_ID"]),
                        USER_NAME = ParseHelper.Parse<string>(row["USER_NAME"]),
                    };
                });
            return fileObj;

        }

        public TB_PERSON_FILEInfo GetFileInfoBy_PathAndName(string path,string name,string userId)
        {
            StringBuilder strSql = new StringBuilder();
            //strSql.Append(@"select a.*,b.obj_name as bus_obj_name,c.obj_name as target_obj_name,e.user_name from tb_person_file a left join tb_tree_bus b on a.biz_obj = b.obj_id left join tb_tree_target c on a.dest_obj =c.obj_id left join base_userinfo e on a.user_id=e.user_id WHERE a.file_name = '" + name.Trim() + "' and a.SAVE_PATH ='" + path.Trim() + "' and a.USER_ID='" + userId + "'");
            strSql.Append(@"select a.*,e.user_name from tb_person_file a left join base_userinfo e on a.user_id=e.user_id  WHERE a.file_name = '" + name.Trim() + "' and a.SAVE_PATH ='" + path.Trim() + "' and a.USER_ID='" + userId + "'");
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString());
            cmd.DbCmdInfo.CommandType = CommandType.Text;
            TB_PERSON_FILEInfo fileObj = DbManager.GetInfo(cmd,
                delegate(out TB_PERSON_FILEInfo u, DataRow row)
                {
                    u = new TB_PERSON_FILEInfo
                    {
                        FILE_ID = ParseHelper.Parse<decimal>(row["FILE_ID"]), 
                        FILE_NAME = ParseHelper.Parse<string>(row["FILE_NAME"]),
                        FILE_EXT = ParseHelper.Parse<string>(row["FILE_EXT"]),
                        FILE_CRT_DATE = ParseHelper.Parse<DateTime>(row["FILE_CRT_DATE"]),
                        KEYW = ParseHelper.Parse<string>(row["KEYW"]),
                        UPD_TIME = ParseHelper.Parse<DateTime>(row["UPD_TIME"]),
                        FILE_TYPE = ParseHelper.Parse<string>(row["FILE_TYPE"]),
                        FILE_ABST = ParseHelper.Parse<string>(row["FILE_ABST"]),
                        CREATE_TIME = ParseHelper.Parse<DateTime>(row["CREATE_TIME"]),
                        SAVE_PATH = ParseHelper.Parse<string>(row["SAVE_PATH"]),
                        USER_ID = ParseHelper.Parse<string>(row["USER_ID"]),
                        FILE_AUTHOR = ParseHelper.Parse<string>(row["FILE_AUTHOR"]),
                        FILE_VERSION = ParseHelper.Parse<decimal>(row["FILE_VERSION"]),
                        FILE_SIZE = ParseHelper.Parse<string>(row["FILE_SIZE"]),
                        DOWN_AMOUNT = ParseHelper.Parse<decimal>(row["DOWN_AMOUNT"]),
                        DEST_OBJ = ParseHelper.Parse<string>(row["DEST_OBJ"]),
                        BIZ_OBJ = ParseHelper.Parse<string>(row["BIZ_OBJ"]),
                        SECRET_LVL = ParseHelper.Parse<string>(row["SECRET_LVL"]),
                        LANG_TYPE = ParseHelper.Parse<decimal>(row["LANG_TYPE"]),
                        PDF_PATH = ParseHelper.Parse<string>(row["PDF_PATH"]),
                        SWF_PATH = ParseHelper.Parse<string>(row["SWF_PATH"]),
                        //Target_obj_name = ParseHelper.Parse<string>(row["target_obj_name"]),
                        //Bus_obj_name = ParseHelper.Parse<string>(row["bus_obj_name"]),
                    };
                });
            return fileObj;

        }

        public bool DeleteBy_PathAndName(string path, string name,string userId)
        {
            try
            {
                StringBuilder strSql = new StringBuilder();

                strSql.Append("delete  FROM TB_PERSON_FILE WHERE file_name=:p_FILE_NAME and save_path=:p_SAVE_PATH and user_id=:p_USER_ID");
                DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString(),
                    delegate()
                    {
                        return DbCmdHelper.CreateParameters(dbProvider, "FILE_NAME", "SAVE_PATH","USER_ID").ToArray();
                    });
                cmd.DbCmdInfo.CommandType = CommandType.Text;
                cmd.SetParamValues(name.Trim(),path.Trim(),userId.Trim());
                return DbManager.ExecNonQuery(cmd) > 0;

                //strSql.Append(@"delete  FROM TB_PERSON_FILE WHERE file_name = '" + name.Trim() + "' and SAVE_PATH ='" + path.Trim() + "'");
                ////strSql.Append("delete from TB_PERSON_FILE where file_id=31");
                //DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString());
                //return (1 == DbManager.ExecNonQuery(cmd));
            }
            catch (Exception ex)
            {
                string str = ex.Message;
            }
            return false;
        }

        /// <summary>
        /// 修改源数据
        /// </summary>
        /// <param name="model">实体对象</param>
        /// <returns>成功返回true,失败返回false</returns>
        public bool UpDateSourceDataBySql(TB_PERSON_FILEInfo model)
        {
            string strSQL = @" UPDATE TB_PERSON_FILE 
					SET FILE_NAME = :p_FILE_NAME,FILE_AUTHOR = :p_FILE_AUTHOR,KEYW = :p_KEYW,FILE_CRT_DATE = :p_FILE_CRT_DATE,DEST_OBJ = :p_DEST_OBJ,BIZ_OBJ = :p_BIZ_OBJ,FILE_ABST=:p_FILE_ABST,FILE_EXT=:p_FILE_EXT,LANG_TYPE=:p_LANG_TYPE,FILE_VERSION=:p_FILE_VERSION,SECRET_LVL=:p_SECRET_LVL
					WHERE FILE_ID=:p_FILE_ID ";
            bool result = true;

            //新建一个数据库操作帮助类
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSQL,
                //参数生成
                delegate()
                {
                    return DbCmdHelper.CreateParameters(dbProvider, "FILE_ID", "FILE_NAME", "FILE_AUTHOR", "KEYW", "FILE_CRT_DATE", "DEST_OBJ", "BIZ_OBJ", "FILE_ABST", "FILE_EXT", "LANG_TYPE", "FILE_VERSION", "SECRET_LVL").ToArray();
                });
            //参数设值
            cmd.SetParamValues(model.FILE_ID, model.FILE_NAME, model.FILE_AUTHOR, model.KEYW, model.FILE_CRT_DATE, model.DEST_OBJ, model.BIZ_OBJ, model.FILE_ABST, model.FILE_EXT,model.LANG_TYPE,model.FILE_VERSION,model.SECRET_LVL);
            cmd.DbCmdInfo.CommandType = CommandType.Text;
            int back = DbManager.ExecNonQuery(cmd);
            if (back == 0)
                result = false;

            return result;

        }

        /// <summary>
        /// 修改关键词和摘要信息
        /// </summary>
        /// <param name="model">实体对象</param>
        /// <returns>成功返回true,失败返回false</returns>
        public bool UpDateInfoBySql(TB_PERSON_FILEInfo model,string types)
        {
            bool result = true;
            string strSQL = "";
            if (types.Equals("ZYXX"))
            {
                strSQL = @" UPDATE TB_PERSON_FILE 
					set FILE_ABST=:p_FILE_ABST
					WHERE FILE_ID=:p_FILE_ID ";
               

                //新建一个数据库操作帮助类
                DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSQL,
                    //参数生成
                    delegate()
                    {
                        return DbCmdHelper.CreateParameters(dbProvider, "FILE_ID", "FILE_ABST").ToArray();
                    });
                //参数设值
                cmd.SetParamValues(model.FILE_ID, model.FILE_ABST);
                cmd.DbCmdInfo.CommandType = CommandType.Text;
                int back = DbManager.ExecNonQuery(cmd);
                if (back == 0)
                    result = false;

                
            }
            else if (types.Equals("GJC"))
            {
                strSQL = @" UPDATE TB_PERSON_FILE 
					set KEYW = :p_KEYW
					WHERE FILE_ID=:p_FILE_ID ";
               

                //新建一个数据库操作帮助类
                DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSQL,
                    //参数生成
                    delegate()
                    {
                        return DbCmdHelper.CreateParameters(dbProvider, "FILE_ID", "KEYW").ToArray();
                    });
                //参数设值
                cmd.SetParamValues(model.FILE_ID, model.KEYW);
                cmd.DbCmdInfo.CommandType = CommandType.Text;
                int back = DbManager.ExecNonQuery(cmd);
                if (back == 0)
                    result = false;

               
            }
            return result;

        }

       /// <summary>
       /// 获取文件总数
       /// </summary>
       /// <param name="folder_id">文件夹ID</param>
       /// <param name="keyword">关键词</param>
       /// <returns></returns>
        public int GetCountByDestObjByKeyWord(string folder_id,string keyword)
        {

            StringBuilder strSql_ = new StringBuilder();
            strSql_.Append("SELECT count(*) FROM TB_PERSON_FILE where  FOLDER_ID = '" + folder_id + "' and  FILE_NAME like '%" + keyword + "%' ");
            DbCmdHelper cmd_ = new DbCmdHelper(dbProvider, strSql_.ToString());
            cmd_.DbCmdInfo.CommandType = CommandType.Text;
            object obj = DbManager.ExecScalar(cmd_);
            return Convert.ToInt32(obj);

        }

        /// <summary>
        /// 获取文件列表
        /// </summary>
        /// <param name="DestObjId">文件所在目标ID</param>
        /// <returns>文件列表</returns>
        public int GetCountByDestObj(string folder_id)
        {

            StringBuilder strSql_ = new StringBuilder();
            strSql_.Append("SELECT count(*) FROM TB_PERSON_FILE where  FOLDER_ID = '" + folder_id + "' ");
            DbCmdHelper cmd_ = new DbCmdHelper(dbProvider, strSql_.ToString());
            cmd_.DbCmdInfo.CommandType = CommandType.Text;
            object obj = DbManager.ExecScalar(cmd_);
            return Convert.ToInt32(obj);

        }

        /// <summary>
        /// 获取文件列表
        /// </summary>
        /// <param name="DestObjId">文件所在目标ID</param>
        /// <returns>文件列表</returns>
        public DataTable GetPersonFileByDestObj(string folder_id)
        {
            StringBuilder strSql_ = new StringBuilder();
            strSql_.Append("SELECT * FROM TB_PERSON_FILE where  FOLDER_ID = '" + folder_id + "' ");
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql_.ToString());

            cmd.DbCmdInfo.CommandType = CommandType.Text;
            DataTable ds = DbHelper.ExecFullDataTable(cmd.DbProvider, cmd.DbCmdInfo);
            if (ds != null)
            {
                return ds.Copy();
            }
            return null;

        }

        /// <summary>
        /// 获取文件列表
        /// </summary>
        /// <param name="DestObjId">文件所在目标ID</param>
        /// <returns>文件列表</returns>
        public DataTable GetTeamFileByDestObj(string folder_id)
        {
            StringBuilder strSql_ = new StringBuilder();
            strSql_.Append("SELECT * FROM TB_TEAM_FILE where  FOLDER_ID = '" + folder_id + "' ");
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql_.ToString());

            cmd.DbCmdInfo.CommandType = CommandType.Text;
            DataTable ds = DbHelper.ExecFullDataTable(cmd.DbProvider, cmd.DbCmdInfo);
            if (ds != null)
            {
                return ds.Copy();
            }
            return null;

        }

        /// <summary>
        /// 获取文件列表
        /// </summary>
        /// <param name="DestObjId">文件所在目标ID</param>
        /// <returns>文件列表</returns>
        public DataTable GetModelByDestObjAndType(string folder_id, string Type, int startNum, int endNum, string ID, ref int total)
        {

            StringBuilder strSql_ = new StringBuilder();
            strSql_.Append("SELECT count(*) FROM TB_PERSON_FILE where  FOLDER_ID = '" + folder_id + "' and  USER_ID='" + ID + "'and is_del=0");
            DbCmdHelper cmd_ = new DbCmdHelper(dbProvider, strSql_.ToString());
            cmd_.DbCmdInfo.CommandType = CommandType.Text;
            object obj = DbManager.ExecScalar(cmd_);

            total = Convert.ToInt32(obj);


            StringBuilder strSql = new StringBuilder();
            if (Type.Equals("FILE_SIZE"))
            {
                //strSql.Append("SELECT * FROM (SELECT  ROWNUM AS rowno,  FILE_NAME,FILE_ID,CREATE_TIME,UPD_TIME,DEST_OBJ,BIZ_OBJ,FILE_SIZE,FILE_EXT FROM TB_PERSON_FILE WHERE FOLDER_ID = '" + folder_id + "' and  USER_ID='" + ID + "') table_tmp WHERE table_tmp.rowno > " + startNum.ToString() + " and table_tmp.rowno <" + endNum.ToString() + " order by tokb(file_size) desc");
                strSql.Append("select * from(SELECT  ROWNUM AS rowno, FILE_NAME,FILE_ID,CREATE_TIME,UPD_TIME,DEST_OBJ,BIZ_OBJ,FILE_SIZE,FILE_EXT from (SELECT  FILE_NAME,FILE_ID,CREATE_TIME,UPD_TIME,DEST_OBJ,BIZ_OBJ,FILE_SIZE,FILE_EXT FROM TB_PERSON_FILE WHERE FOLDER_ID = '" + folder_id + "' and  USER_ID='" + ID + "' and is_del=0  order by tokb(file_size) desc))table_tmp WHERE table_tmp.rowno > " + startNum.ToString() + " and table_tmp.rowno <" + endNum.ToString());
            }
            else
            {
                //strSql.Append("SELECT * FROM (SELECT  ROWNUM AS rowno,  FILE_NAME,FILE_ID,CREATE_TIME,UPD_TIME,DEST_OBJ,BIZ_OBJ,FILE_SIZE,FILE_EXT FROM TB_PERSON_FILE WHERE FOLDER_ID = '" + folder_id + "' and  USER_ID='" + ID + "') table_tmp WHERE table_tmp.rowno > " + startNum.ToString() + " and table_tmp.rowno <" + endNum.ToString() + " order by " + Type + " desc");
                strSql.Append("select * from(SELECT ROWNUM AS rowno,  FILE_NAME,FILE_ID,CREATE_TIME,UPD_TIME,DEST_OBJ,BIZ_OBJ,FILE_SIZE,FILE_EXT  FROM (SELECT  FILE_NAME,FILE_ID,CREATE_TIME,UPD_TIME,DEST_OBJ,BIZ_OBJ,FILE_SIZE,FILE_EXT FROM TB_PERSON_FILE WHERE FOLDER_ID = '" + folder_id + "' and  USER_ID='" + ID + "' and is_del=0 order by " + Type + " desc) )table_tmp WHERE table_tmp.rowno > " + startNum.ToString() + " and table_tmp.rowno <" + endNum.ToString());
            }
             DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString());

            cmd.DbCmdInfo.CommandType = CommandType.Text;
            DataTable ds = DbHelper.ExecFullDataTable(cmd.DbProvider, cmd.DbCmdInfo);
            if (ds != null)
            {
                return ds.Copy();
            }
            return null;

        }
        
      /// <summary>
      /// 搜索文件列表
      /// </summary>
      /// <param name="folder_id">文件夹ID</param>
      /// <param name="startNum"></param>
      /// <param name="endNum"></param>
      /// <param name="ID">用户ID</param>
      /// <param name="keyword">关键词</param>
      /// <param name="total"></param>
      /// <returns></returns>
        public DataTable GetModelByDestObjAndKeyWord(string folder_id, int startNum, int endNum, string ID,string keyword, ref int total)
        {

            StringBuilder strSql_ = new StringBuilder();
            strSql_.Append("SELECT count(*) FROM TB_PERSON_FILE where  FOLDER_ID = '" + folder_id + "' and  USER_ID='" + ID + "' and FILE_NAME like '%" + keyword + "%'");
            DbCmdHelper cmd_ = new DbCmdHelper(dbProvider, strSql_.ToString());
            cmd_.DbCmdInfo.CommandType = CommandType.Text;
            object obj = DbManager.ExecScalar(cmd_);
            total = Convert.ToInt32(obj);


            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM (SELECT  ROWNUM AS rowno,  FILE_NAME,FILE_ID,CREATE_TIME,UPD_TIME,DEST_OBJ,FILE_SIZE FROM TB_PERSON_FILE WHERE FOLDER_ID = '" + folder_id + "' and  USER_ID='" + ID + "' and is_del=0 and FILE_NAME like '%" + keyword + "%') table_tmp WHERE table_tmp.rowno > " + startNum.ToString() + " and table_tmp.rowno <" + endNum.ToString() + " order by Create_time desc");
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString());

            cmd.DbCmdInfo.CommandType = CommandType.Text;
            DataTable ds = DbHelper.ExecFullDataTable(cmd.DbProvider, cmd.DbCmdInfo);
            if (ds != null)
            {
                return ds.Copy();
            }
            return null;

        }


        /// <summary>
        /// 获取文件列表
        /// </summary>
        /// <param name="DestObjId">文件所在目标ID</param>
        /// <returns>文件列表</returns>
        public DataTable GetModelByDestObj(string folder_id, int startNum, int endNum, string ID, ref int total)
        {

            StringBuilder strSql_ = new StringBuilder();
            strSql_.Append("SELECT count(*) FROM TB_PERSON_FILE where  FOLDER_ID = '" + folder_id + "' and  USER_ID='" + ID + "'");
            DbCmdHelper cmd_ = new DbCmdHelper(dbProvider, strSql_.ToString());
            cmd_.DbCmdInfo.CommandType = CommandType.Text;
            object obj = DbManager.ExecScalar(cmd_);
            total = Convert.ToInt32(obj);


            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM (SELECT  ROWNUM AS rowno,  FILE_NAME,FILE_ID,CREATE_TIME,UPD_TIME,DEST_OBJ,FILE_SIZE FROM TB_PERSON_FILE WHERE FOLDER_ID = '" + folder_id + "' and  USER_ID='" + ID + "' and is_del=0 ) table_tmp WHERE table_tmp.rowno > " + startNum.ToString() + " and table_tmp.rowno <" + endNum.ToString() + " order by Create_time desc");
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString());

            cmd.DbCmdInfo.CommandType = CommandType.Text;
            DataTable ds = DbHelper.ExecFullDataTable(cmd.DbProvider, cmd.DbCmdInfo);
            if (ds != null)
            {
                return ds.Copy();
            }
            return null;

        }
        //数据库是否有文件
        public bool IsMoreThanOne(string File_ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT count(*) FROM TB_XMK_FILE WHERE File_ID = '" + File_ID + "'");
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString());

            cmd.DbCmdInfo.CommandType = CommandType.Text;
            object obj = DbManager.ExecScalar(cmd);

            return (Convert.ToInt16(obj) > 1);
        }

      //根据文件ID删除记录
        public bool DeleteBySql(string File_ID)
        {
            try
            {
                StringBuilder strSql = new StringBuilder();

                strSql.Append("delete  FROM TB_PERSON_FILE WHERE File_ID=:p_File_ID");
                DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString(),
                    delegate()
                    {
                        return DbCmdHelper.CreateParameters(dbProvider, "File_ID").ToArray();
                    });
                cmd.DbCmdInfo.CommandType = CommandType.Text;
                cmd.SetParamValues(File_ID);
                return DbManager.ExecNonQuery(cmd) > 0;

            }
            catch (Exception ex)
            {
                string str = ex.Message;
            }
            return false;
        }
        /// <summary>
        /// 获取回收站信息
        /// </summary>
        /// <param name="strWhere">条件</param>
        /// <returns>结果集</returns>
        public DataTable GetRecycleDataBySql(string User_ID, string sorttype, string sortdesc, int startNum, int endNum, ref int total)
        {
            StringBuilder strSql_ = new StringBuilder();
            strSql_.Append("SELECT count(*) FROM TB_PERSON_FILE where  USER_ID='" + User_ID + "' and is_del=1");
            DbCmdHelper cmd_ = new DbCmdHelper(dbProvider, strSql_.ToString());
            cmd_.DbCmdInfo.CommandType = CommandType.Text;
            object obj = DbManager.ExecScalar(cmd_);

            total = Convert.ToInt32(obj);
            StringBuilder strSql = new StringBuilder();
            if (sorttype == "time")
            {
                if (sortdesc == "desc")
                {
                    strSql.Append(@"SELECT * FROM (SELECT  ROWNUM AS rowno,  file_id,file_name,FILE_SIZE,RECYCLE_DATE from(SELECT file_id,file_name,FILE_SIZE,RECYCLE_DATE from tb_person_file where is_del=1 and user_id='" + User_ID + "'order by Create_time desc)) table_tmp WHERE table_tmp.rowno > " + startNum.ToString() + " and table_tmp.rowno <" + endNum.ToString());
                }
                else
                {
                    strSql.Append(@"SELECT * FROM (SELECT  ROWNUM AS rowno,  file_id,file_name,FILE_SIZE,RECYCLE_DATE from(SELECT file_id,file_name,FILE_SIZE,RECYCLE_DATE from tb_person_file where is_del=1 and user_id='" + User_ID + "'order by Create_time asc)) table_tmp WHERE table_tmp.rowno > " + startNum.ToString() + " and table_tmp.rowno <" + endNum.ToString());
                }
            }
            else if (sorttype == "size")
            {
                if (sortdesc == "desc")
                {
                    strSql.Append(@"SELECT * FROM (SELECT  ROWNUM AS rowno,  file_id,file_name,FILE_SIZE,RECYCLE_DATE from(SELECT file_id,file_name,FILE_SIZE,RECYCLE_DATE from tb_person_file where is_del=1 and user_id='" + User_ID + "'order by tokb(FILE_SIZE） desc)) table_tmp WHERE table_tmp.rowno > " + startNum.ToString() + " and table_tmp.rowno <" + endNum.ToString());
                }
                else
                {
                    strSql.Append(@"SELECT * FROM (SELECT  ROWNUM AS rowno,  file_id,file_name,FILE_SIZE,RECYCLE_DATE from(SELECT file_id,file_name,FILE_SIZE,RECYCLE_DATE from tb_person_file where is_del=1 and user_id='" + User_ID + "'order by tokb(FILE_SIZE） asc)) table_tmp WHERE table_tmp.rowno > " + startNum.ToString() + " and table_tmp.rowno <" + endNum.ToString());
                }
            }
           
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString());
            cmd.DbCmdInfo.CommandType = CommandType.Text;
            DataTable ds = DbHelper.ExecFullDataTable(cmd.DbProvider, cmd.DbCmdInfo);
            if (ds != null)
            {
                return ds.Copy();
            }
            return null;
        }

        //根据文件ID将文件移动到回收站
        public bool RemoveToRecycleBySql(string File_ID)
        {
            try
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("update TB_PERSON_FILE set is_del=1,RECYCLE_DATE=to_date('" + System.DateTime.Now.Date.ToString("yyyy-MM-dd") + "','yyyy-MM-dd') where file_id in (" + File_ID + ")");
                DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString());
                cmd.DbCmdInfo.CommandType = CommandType.Text;
                return DbManager.ExecNonQuery(cmd) > 0;

            }
            catch (Exception ex)
            {
                string str = ex.Message;
            }
            return false;
        }


        /// <summary>
        /// 获取文件列表
        /// </summary>
        /// <param name="FileID">文件IDs</param>
        /// <returns>文件列表</returns>
        public DataTable GetModelListBySql(string FileID)
        {
            //获得待审核文件信息
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from TB_PERSON_FILE WHERE FILE_ID in(" + FileID + ")");
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString());

            cmd.DbCmdInfo.CommandType = CommandType.Text;
            DataTable ds = DbHelper.ExecFullDataTable(cmd.DbProvider, cmd.DbCmdInfo);
            if (ds != null)
            {
                return ds.Copy();
            }
            return null;
        }


        public TB_PERSON_FILEInfo GetModelListsBySql(string FileID)
        {
            StringBuilder strSql = new StringBuilder();
            
            strSql.Append("select * from TB_PERSON_FILE WHERE FILE_ID in(" + FileID + ")");
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString());
            cmd.DbCmdInfo.CommandType = CommandType.Text;
            TB_PERSON_FILEInfo fileObj = DbManager.GetInfo(cmd,
                delegate(out TB_PERSON_FILEInfo u, DataRow row)
                {
                    u = new TB_PERSON_FILEInfo
                    {
                        FILE_ID = ParseHelper.Parse<decimal>(row["FILE_ID"]),
                        FILE_NAME = ParseHelper.Parse<string>(row["FILE_NAME"]),
                        FILE_EXT = ParseHelper.Parse<string>(row["FILE_EXT"]),
                        FILE_CRT_DATE = ParseHelper.Parse<DateTime>(row["FILE_CRT_DATE"]),
                        KEYW = ParseHelper.Parse<string>(row["KEYW"]),
                        UPD_TIME = ParseHelper.Parse<DateTime>(row["UPD_TIME"]),
                        FILE_TYPE = ParseHelper.Parse<string>(row["FILE_TYPE"]),
                        FILE_ABST = ParseHelper.Parse<string>(row["FILE_ABST"]),
                        CREATE_TIME = ParseHelper.Parse<DateTime>(row["CREATE_TIME"]),
                        SAVE_PATH = ParseHelper.Parse<string>(row["SAVE_PATH"]),
                        USER_ID = ParseHelper.Parse<string>(row["USER_ID"]),
                        FILE_AUTHOR = ParseHelper.Parse<string>(row["FILE_AUTHOR"]),
                        FILE_VERSION = ParseHelper.Parse<decimal>(row["FILE_VERSION"]),
                        FILE_SIZE = ParseHelper.Parse<string>(row["FILE_SIZE"]),
                        DOWN_AMOUNT = ParseHelper.Parse<decimal>(row["DOWN_AMOUNT"]),
                        DEST_OBJ = ParseHelper.Parse<string>(row["DEST_OBJ"]),
                        BIZ_OBJ = ParseHelper.Parse<string>(row["BIZ_OBJ"]),
                        SECRET_LVL = ParseHelper.Parse<string>(row["SECRET_LVL"]),
                        LANG_TYPE = ParseHelper.Parse<decimal>(row["LANG_TYPE"]),
                        PDF_PATH = ParseHelper.Parse<string>(row["PDF_PATH"]),
                        SWF_PATH = ParseHelper.Parse<string>(row["SWF_PATH"])
                    };
                });
            return fileObj;

        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="fileId">ids</param>
        /// <returns></returns>
        public bool DeleteIN(string fileId)
        {
            bool result = true;


            string sql = @"delete from  TB_PERSON_FILE  where FILE_ID in (" + fileId + ") ";

            //新建一个数据库操作帮助类
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, sql);
               
            cmd.DbCmdInfo.CommandType = CommandType.Text;
            int back = DbManager.ExecNonQuery(cmd);
            return result;
        }

        //根据文件ID将文件从回收站移回空间
        public bool RecycleToSpaceBySql(string File_ID)
        {
            try
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("update TB_PERSON_FILE set is_del=0 where file_id in (" + File_ID + ")");
                DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString());
                cmd.DbCmdInfo.CommandType = CommandType.Text;
                return DbManager.ExecNonQuery(cmd) > 0;

            }
            catch (Exception ex)
            {
                string str = ex.Message;
            }
            return false;
        }

        //获取个人空间同名文件最大版本号
        public decimal GetMaxFileVersion(string fileName,string UserId)
        {

            try
            {

                //获得待审核文件信息
                StringBuilder strSql = new StringBuilder();
                strSql.Append("select max(FILE_VERSION) FROM TB_PERSON_FILE where FILE_NAME='" + fileName + "' and USER_ID='" + UserId + "'");
                DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString());

                cmd.DbCmdInfo.CommandType = CommandType.Text;
                DataTable ds = DbHelper.ExecFullDataTable(cmd.DbProvider, cmd.DbCmdInfo);
                if (ds != null)
                {
                    return ParseHelper.Parse<decimal>(ds.Rows[0][0]);
                } 

            }
            catch (Exception ex)
            {
                string str = ex.Message;
            }
            return 0;
        }
        /// <summary>
        /// 分类查看查询数据
        /// </summary>
        /// <param name="teamId"></param>
        /// <returns></returns>
        public DataTable getXmkClassFlie(string personId, string FOLDER_ID, string classType)
        {
            StringBuilder strSql = new StringBuilder();
            if (classType == "CREATE_TIME")
            {
                strSql.Append("select FILE_ID,FILE_NAME," + classType + " from TB_PERSON_FILE  WHERE FOLDER_ID='" + FOLDER_ID + "' and USER_ID='" + personId + "'order by " + classType + "");
            }
            else
            {
                strSql.Append("select FILE_ID,FILE_NAME,CREATE_TIME," + classType + " from TB_PERSON_FILE  WHERE FOLDER_ID='" + FOLDER_ID + "'  and USER_ID='" + personId + "' order by " + classType + "");
            }


            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString());

            cmd.DbCmdInfo.CommandType = CommandType.Text;
            DataTable ds = DbHelper.ExecFullDataTable(cmd.DbProvider, cmd.DbCmdInfo);
            if (ds != null)
            {
                return ds.Copy();
            }
            return null;
        }

        /// <summary>
        /// 分类查看明细数据
        /// </summary>
        /// <param name="teamId"></param>
        /// <returns></returns>
        public DataTable getXmkClassDetailFile(string personId,string sqlwhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FILE_ID as id,FILE_NAME,fILE_SIZE,CREATE_TIME,SAVE_PATH from TB_PERSON_FILE  WHERE " + sqlwhere + " and USER_ID='" + personId + "'");

            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString());

            cmd.DbCmdInfo.CommandType = CommandType.Text;
            DataTable ds = DbHelper.ExecFullDataTable(cmd.DbProvider, cmd.DbCmdInfo);
            if (ds != null)
            {
                return ds.Copy();
            }
            return null;
        }

        /// <summary>
        /// 获取文档Model
        /// </summary>
        /// <param name="FileID">文件ID</param>
        /// <returns>文件列表</returns>
        public XMKFileExtInfo GetModelByFileId(string FileID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select a.* from tb_person_file a WHERE a.FILE_ID = ' " + FileID + "'");
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString());

            cmd.DbCmdInfo.CommandType = CommandType.Text;
            return DbManager.GetInfo(cmd,
                delegate(out XMKFileExtInfo model, DataRow row)
                {
                    model = new XMKFileExtInfo
                    {
                        FILE_ID = ParseHelper.Parse<long>(row["FILE_ID"]),
                        FILE_NAME = ParseHelper.Parse<string>(row["FILE_NAME"]),
                        FILE_EXT = ParseHelper.Parse<string>(row["FILE_EXT"]),
                        KEYW = ParseHelper.Parse<string>(row["KEYW"]),
                        UPD_TIME = ParseHelper.Parse<DateTime>(row["UPD_TIME"]),
                        FILE_TYPE = ParseHelper.Parse<string>(row["FILE_TYPE"]),
                        FILE_ABST = ParseHelper.Parse<string>(row["FILE_ABST"]),
                        CREATE_TIME = ParseHelper.Parse<DateTime>(row["CREATE_TIME"]),
                        SAVE_PATH = ParseHelper.Parse<string>(row["SAVE_PATH"]),
                        USER_ID = ParseHelper.Parse<string>(row["USER_ID"]),
                        FILE_AUTHOR = ParseHelper.Parse<string>(row["FILE_AUTHOR"]),
                        FILE_VERSION = ParseHelper.Parse<string>(row["FILE_VERSION"]),
                        FILE_SIZE = ParseHelper.Parse<string>(row["FILE_SIZE"]),
                        DOWN_AMOUNT = ParseHelper.Parse<decimal>(row["DOWN_AMOUNT"]),
                        DEST_OBJ = ParseHelper.Parse<string>(row["DEST_OBJ"]),
                        BIZ_OBJ = ParseHelper.Parse<string>(row["BIZ_OBJ"]),
                        SECRET_LVL = ParseHelper.Parse<string>(row["SECRET_LVL"]),
                        LANG_TYPE = ParseHelper.Parse<string>(row["LANG_TYPE"]),
                        BIZ_OBJ_NAME = ParseHelper.Parse<string>(row["biz_obj"]),
                        DEST_OBJ_NAME = ParseHelper.Parse<string>(row["dest_obj"]),
                        PDFFilePath = ParseHelper.Parse<string>(row["PDF_PATH"]),
                        PRE_PIC = ParseHelper.Parse<string>(row["PRE_PIC"]),
                        SwfFilePath = ParseHelper.Parse<string>(row["SWF_PATH"])
                    };
                });

        }

    }
}
